//
//  ZDoctorViewController.m
//  DoctorFAQs
//
//  Created by zjq on 17/2/20.
//  Copyright © 2017年 zjq. All rights reserved.
//

#import "ZDoctorViewController.h"
#import "ZDoctorVCHeadView.h"

#import "ZDoctorHeadCell.h"
#import "ZFAQsTableViewCell.h"
#import "ZCommentTableViewCell.h"

#import "ZDoctorAnswerViewController.h"
#import "ZDiscussViewController.h"
#import "ZDoctorPaperViewController.h"
#import "ZVideoViewController.h"
#import "ZLookFriendDeatilViewController.h"

#import "ZZNewCommentViewController.h"// 医生的评论

#import "ZDctorcNModel.h"
#import "ZDoctorHModel.h"
#import "TacticsDetailModel.h"
static NSString * const KdoctorHeadCellIdentifier = @"doctorHeadCellIdentifier";
static NSString * const KFAQsTableViewCellIdentifier = @"FAQsTableViewCellIdentifier";
static NSString * const KcommentTableViewCellIdentifier = @"commentTableViewCellIdentifier";
@interface ZDoctorViewController ()<UITableViewDelegate,UITableViewDataSource,UINavigationControllerDelegate> {
    ZDoctorVCHeadView * _doctorHeadView;
    UITableView * _mainTableView;
    UIView * _alphaview;// 用来装 系统的bar的地址
}
@property (nonatomic,strong) ZDctorcNModel * dModel;
@property (nonatomic,strong) NSMutableArray <ZDoctorHModel *>* hModelArray;
@property (nonatomic,strong) NSMutableArray <TacticsDetailModel *> * tModelArray;
@end

@implementation ZDoctorViewController
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self gradualChangeNavBarWith:YES];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [self gradualChangeNavBarWith:NO];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.delegate = self;
    [self initialzeDatas];
    [self initlizeInterface];
    
}
- (void)initialzeDatas {
    _hModelArray = [@[] mutableCopy];
    _tModelArray = [@[] mutableCopy];
}

- (void)initlizeInterface {
    
    [self makeAllSubView];
    [self addAllSubView];
}

- (void)makeAllSubView {
    _doctorHeadView = [[ZDoctorVCHeadView alloc] init];
    _doctorHeadView.frame = CGRectMake(0, 0, SCREEN_Width, 230*ysIZE);
    [_doctorHeadView.wenzhangBtn addTarget:self action:@selector(jumpWenZhang) forControlEvents:UIControlEventTouchUpInside];
    [_doctorHeadView.videoBtn addTarget:self action:@selector(jumpVideo) forControlEvents:UIControlEventTouchUpInside];
    _mainTableView = [[UITableView alloc] initWithFrame:CGRectMake(0,-64, SCREEN_Width, SCREEN_Height) style:UITableViewStyleGrouped];
    _mainTableView.delegate = self;
    _mainTableView.dataSource = self;
    _mainTableView.tableHeaderView = _doctorHeadView;
    _mainTableView.sectionFooterHeight = 0;
    _mainTableView.sectionHeaderHeight = 0;// 设置组间的间距为0
    _mainTableView.bounces = NO;
    _mainTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [_mainTableView registerClass:[ZDoctorHeadCell class] forCellReuseIdentifier:KdoctorHeadCellIdentifier];
    [_mainTableView registerClass:[ZFAQsTableViewCell class] forCellReuseIdentifier:KFAQsTableViewCellIdentifier];
    [_mainTableView registerClass:[ZCommentTableViewCell class] forCellReuseIdentifier:KcommentTableViewCellIdentifier];
    [_mainTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"tabViewCell"];

    
}

- (void)addAllSubView {
    [self.view addSubview:_mainTableView];
}

- (void)setDid:(NSString *)did {
    _did = did;
    [self doctorNetWorking];
}
- (void)setDModel:(ZDctorcNModel *)dModel {
    _dModel = dModel;
    _doctorHeadView.model = _dModel;
    [_mainTableView reloadData];
    
}
// 进入文章界面
- (void)jumpWenZhang {
    ZDoctorPaperViewController * ZDPVC = [[ZDoctorPaperViewController alloc] init];
    ZDPVC.did = self.did;
    ZDPVC.navigationItem.title = [NSString stringWithFormat:@"%@的文章",_dModel.name];
    [self.navigationController pushViewController:ZDPVC animated:YES];
}
- (void)jumpVideo {
    ZVideoViewController * ZVVC = [[ZVideoViewController alloc] init];
    ZVVC.did = self.did;
    ZVVC.navigationItem.title = [NSString stringWithFormat:@"%@的视频",_dModel.name];
    [self.navigationController pushViewController:ZVVC animated:YES];
}
// 滚动视图的代理方法
- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    if (scrollView.contentOffset.y > 64) {
        _alphaview.alpha = 1;
    }else if (scrollView.contentOffset.y>=5) {
        _alphaview.alpha = scrollView.contentOffset.y * (1/64.0);
    }else{
        _alphaview.alpha = 0;
    }
    
}
//找到导航栏改变透明度
- (void)gradualChangeNavBarWith:(BOOL)ISSelf {
    for (UIView * view in self.navigationController.navigationBar.subviews) {
        NSString *version = [UIDevice currentDevice].systemVersion;
        if ([version doubleValue]>= 10.0) {
            if ([view isKindOfClass:NSClassFromString(@"_UIBarBackground")]) {
                _alphaview= view;
                
                _alphaview.alpha = ISSelf? 0:1;
                
            }
        }else {
            if ([view isKindOfClass:NSClassFromString(@"_UINavigationBarBackground")]) {
                _alphaview = view;
                _alphaview.alpha = ISSelf ? 0:1;
            }
        }
    }
}
// 表格视图协议
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 2;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section ==0 ) {
        return _hModelArray ? _hModelArray.count+1:1;
    }
      return _tModelArray ? _tModelArray.count+1:1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        return 40*ysIZE;
    }else if (indexPath.section == 1) {
        return 85*ysIZE;
    }
    return 145*ysIZE;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        ZDoctorHeadCell * cell  = [tableView dequeueReusableCellWithIdentifier:KdoctorHeadCellIdentifier forIndexPath:indexPath];
        cell.selectionStyle = 0;
        if (indexPath.section == 0) {
            if (_dModel) {
                cell.midTitleLabel.text =[NSString stringWithFormat:@"%@%@",_dModel.name,@"的回复"];
            }
            
            return cell;
        }
        cell.leftImageView.image = [UIImage imageNamed:@"voice_presentation"];
        cell.midTitleLabel.text = @"用户评论";
        return cell;
    }else if (indexPath.section == 0) {
        ZFAQsTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:KFAQsTableViewCellIdentifier forIndexPath:indexPath];
        if (_hModelArray&&_hModelArray.count != 0) {
            cell.fModel = _hModelArray[indexPath.row-1];

        }
       
        cell.selectionStyle = 0;
        return cell;
    }else if (indexPath.section == 1 ) {
        ZCommentTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:KcommentTableViewCellIdentifier forIndexPath:indexPath];
        if (_tModelArray &&_tModelArray.count != 0) {
            cell.tacticsDetailModel = _tModelArray[indexPath.row -1];
        }
        
        cell.selectionStyle = 0;
        return cell;
    }
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"tableViewCell" forIndexPath:indexPath];
    return cell;
    
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0) {
        ZDoctorAnswerViewController * vc = [[ZDoctorAnswerViewController alloc]init];
         vc.navigationItem.title = [NSString stringWithFormat:@"%@%@",_dModel.name,@"的回复"];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.section == 1 &&indexPath.row == 0) {
        ZZNewCommentViewController * vc = [[ZZNewCommentViewController alloc] init];
        vc.did = _dModel.did;
        vc.navigationItem.title = [NSString stringWithFormat:@"%@%@",_dModel.name,@"的评论"];
        [self.navigationController pushViewController:vc animated:YES];
    }else if (indexPath.section == 0&& indexPath.row != 0) {
        ZDoctorHModel * model = _hModelArray[indexPath.row];
        ZLookFriendDeatilViewController * deVc = [[ZLookFriendDeatilViewController alloc] init];
        deVc.qId = model.qid;
        [self .navigationController pushViewController:deVc animated:YES];
    }
}

- (void)doctorNetWorking {
    NSDictionary * dict = @{@"did":self.did};
    [YJProgressHUD showProgress:@"正在加载" inView:self.view];
    [BaseRequest requestWithMethodResponseJsonByGet:TestURL paramars:dict paramarsSite:@"Consult/doctorHome" sucessBlock:^(id content) {
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:content options:NSJSONReadingMutableContainers error:nil];
        if ([dict[@"code"] intValue] ==1) {
            [YJProgressHUD showMessage:@"加载成功" inView:self.view];
            [self hanldDataWithDict:dict[@"list"]];
        }else {
            [YJProgressHUD showMessage:@"加载失败" inView:self.view];
        }
    } failure:^(NSError *error) {
        [YJProgressHUD showMessage:@"网络请求失败" inView:self.view];
    }];
}
- (void)hanldDataWithDict:(NSDictionary *)dict {
    LZLog(@"%@",dict);
    self.dModel = [[ZDctorcNModel alloc] initWithDict:[dict[@"info"] mutableCopy]];
    NSArray * answerArray = dict[@"answer"];
    [_hModelArray removeAllObjects];
    for (NSMutableDictionary * qDict in answerArray) {
        ZDoctorHModel * model = [[ZDoctorHModel alloc] initWithDict:qDict];
        [_hModelArray addObject:model];
    }
    NSArray * pjArray = dict[@"pj"];
    [_tModelArray removeAllObjects];
    for (NSMutableDictionary * pDict in pjArray) {
        TacticsDetailModel * model = [[TacticsDetailModel alloc] initWithDict:pDict];
        [_tModelArray addObject:model];
    }
    
    [_mainTableView reloadData];
}

//- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
//    // 判断要显示的控制器是否是自己
//    BOOL isShowHomePage = [viewController isKindOfClass:[self class]];
//    if (isShowHomePage) {
//        [self gradualChangeNavBarWith:YES];
//    }else {
//        [self gradualChangeNavBarWith:NO];
//    }
//    
//}


@end
